package com.demo.controller;

import clojure.lang.Obj;
import com.demo.enums.CycleTypeEnum;
import com.demo.model.User;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

@Controller
@RequestMapping("/")
public class IndexController {

    Logger logger = Logger.getLogger(getClass());
    Gson gson = new Gson();

    @RequestMapping("")
    @ResponseBody
    public Object index(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        logger.info("----index----");

        MultipartFile multipartFile = ((MultipartHttpServletRequest) request)
                .getFile("f1");

        Scanner scanner = new Scanner(multipartFile.getInputStream());
        StringBuilder s = new StringBuilder();
        while (scanner.hasNextLine()) {
            s.append(scanner.nextLine());
        }
        printHalvingLine();
        System.out.println(s);
        printHalvingLine();
        return s;
    }


    @RequestMapping("/hi")
    public ModelAndView hi(HttpServletRequest request,HttpServletResponse response) {
        printHalvingLine();
        Cookie[] cookies = request.getCookies();
        Cookie cookie = new Cookie("xiaomiId","12345");

        response.addCookie(cookie);
        ModelAndView mav = new ModelAndView("hello");
        mav.addObject("user", new User());
        printHalvingLine();
        return mav;
    }



    @RequestMapping("/test1")
    @ResponseBody
    public Object test1(Date date) {
        printHalvingLine();

        System.out.println("date = " + date);

        printHalvingLine();
        return "";
    }

    @RequestMapping("/404")
    @ResponseBody
    public Object notFound(String json) {
        String msg = "page  not  found!";
        return this;
    }

    @RequestMapping("/500")
    @ResponseBody
    public Object sysError(String json) {
        String msg = "system error!";
        return msg;
    }

    public static void main(String[] args) {

    }

    static void printHalvingLine() {
        System.out.println("------------------------------");
        System.out.println("------------------------------");
    }
}
